<template>
  <div>
    <div id="simpleForm" style="display:none">
      <div class="col-xs-12">
        <div class="card">
          <div class="card-header">
            <div class="card-title">
              <div class="title">
                <div v-if="isAdd">{{formconfigEntity.add_form_name}}</div>
                <div v-if="!isAdd">{{formconfigEntity.edit_form_name}}</div>
              </div>
            </div>
          </div>
          <div class="card-body">
            <template v-for="item in formconfigEntity.formConfigs">
              <div class="form-group" v-if="item.inputType=='input'">
                <label>{{item.displayName}}</label>
                <input class="form-control" v-model="item.value"/>
              </div>
              <div class="form-group" v-if="item.inputType=='textarea'">
                <label>{{item.displayName}}</label>
                <textarea class="form-control" v-model="item.value"></textarea>
              </div>
              <div class="form-group" v-if="item.inputType=='selector'">
                <label>{{item.displayName}}</label>
                <select class="form-control" v-model="item.value">
                  <option value="" v-if="!item.isRequire">请选择</option>
                  <template v-for="entity in item.datasource">
                    <option value="{{entity.key}}">{{entity.value}}</option>
                  </template>
                </select>
              </div>
            </template>
            <div class="pull-right">
              <button type="submit" class="btn btn-info" @click="submitForm()">提交</button>
              <button type="submit" class="btn btn-default" @click="closeDialog()">关闭</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  import request from 'superagent';
  export default{
    props: ['formconfigEntity', 'isAdd', 'fid'],
    data(){
      return {}
    },
    methods: {
      "closeDialog": function () {
        layer.closeAll();
      },
      "submitForm": function () {
        let _self = this;
        let formContent = JSON.stringify(this.formconfigEntity);

        let canSubmit = true;
        let errMsg = "";
        //校验表单
        for (let i = 0; i < this.formconfigEntity.formConfigs.length; i++) {
          if (this.formconfigEntity.formConfigs[i].isRequire == true) {
            if (this.formconfigEntity.formConfigs[i].value == "") {
              errMsg += "请填写" + this.formconfigEntity.formConfigs[i].displayName + ";<br/>";
              canSubmit = false;
            }
          }
        }

        if (canSubmit) {
          request
            .get(window.serverUrl + 'admin/simplegrid/submitEntity')
            .query({uid: this.$route.params.key})
            .query({id: this.fid})
            .query({content: formContent})
            .end(function (err, res) {
              layer.closeAll();
              if (_self.isAdd) {
                _self.$dispatch('submitFinish', "新增成功");
              } else {
                _self.$dispatch('submitFinish', "修改成功");
              }
            });
        } else {
          layer.alert(errMsg);
        }
      }
    },
    components: {}
  }
</script>
